package com.github.niefy.modules.wx.controller;

import com.github.niefy.common.utils.R;
import com.github.niefy.modules.wx.dto.weiboconfig.WeiboConfigDTO;
import com.github.niefy.modules.wx.entity.WeiboConfig;
import com.github.niefy.modules.wx.entity.WxAccount;
import com.github.niefy.modules.wx.entity.common.CommonQuery;
import com.github.niefy.modules.wx.service.WeiboConfigService;
import com.github.niefy.modules.wx.service.WxAccountService;
import com.github.niefy.modules.wx.vo.weiboconfig.WeiboConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 微信用户（粉丝）
 */
@RestController
@RequiredArgsConstructor
@Api(tags = {"微博配置"})
public class WeiboConfigController {
    @Autowired
    private WeiboConfigService weiboConfigService;
    @Autowired
    private WxAccountService wxAccountService;

    @PostMapping("/weiBoConfig/page")
    @ApiOperation(value = "列表")
    public R page(@RequestBody CommonQuery<WeiboConfig> page) {

        return R.ok().put(weiboConfigService.pageService(page));
    }

    @PostMapping("/weiBoConfig/save")
    @ApiOperation(value = "列表")
    public R save(@RequestBody WeiboConfig input) {
        return R.ok().put(weiboConfigService.saveService(input));
    }

    @PostMapping("/weiBoConfig/update")
    @ApiOperation(value = "列表")
    public R update(@RequestBody WeiboConfig input) {
        return R.ok().put(weiboConfigService.updateService(input));
    }

    @PostMapping("/weiBoConfig/getById")
    @ApiOperation(value = "列表")
    public R getById(@RequestBody WeiboConfigDTO input) {
        WeiboConfig weiboConfig = weiboConfigService.getByIdService(input);
        WxAccount wxAccount = wxAccountService.getByAppId(weiboConfig.getWxAppId());

        WeiboConfigVO weiboConfigVO = new WeiboConfigVO();
        BeanUtils.copyProperties(weiboConfig, weiboConfigVO);
        weiboConfigVO.setWxAccountName(wxAccount.getName());

        return R.ok().put(weiboConfigVO);
    }

    @PostMapping("/weiBoConfig/disable")
    @ApiOperation(value = "列表")
    public R disable(@RequestBody WeiboConfigDTO input) {
        return R.ok().put(weiboConfigService.disableService(input));
    }

    @PostMapping("/weiBoConfig/delete")
    @ApiOperation(value = "列表")
    public R delete(@RequestBody WeiboConfigDTO input) {
        return R.ok().put(weiboConfigService.deleteService(input));
    }

    @PostMapping("/weiBoConfig/updateNormal")
    @ApiOperation(value = "列表")
    public R updateNormal(@RequestBody WeiboConfigDTO input) {
        return R.ok().put(weiboConfigService.updateNormal(input));
    }
}
